home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / PartMaker 4.3 / DoEvent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-12  |  6.5 KB  |  240 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        DoEvent.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19.  
  20.  
  21. /*****************************************************************************/
  22.  
  23.  
  24.  
  25. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  26. #include "App.defs.h"        /* Get various application definitions.            */
  27. #include "App.protos.h"        /* Get the prototypes for application.            */
  28.  
  29. #ifndef __CTLHANDLER__
  30. #include "CtlHandler.h"
  31. #endif
  32.  
  33. #ifndef __DESK__
  34. #include <Desk.h>
  35. #endif
  36.  
  37. #ifndef __DISKINIT__
  38. #include <DiskInit.h>
  39. #endif
  40.  
  41. #ifndef __ERRORS__
  42. #include <Errors.h>
  43. #endif
  44.  
  45. #ifndef __MENUS__
  46. #include <Menus.h>
  47. #endif
  48.  
  49. #ifndef __TEXTEDITCONTROL__
  50. #include "TextEditControl.h"
  51. #endif
  52.  
  53. #ifndef __TEXTSERVICES__
  54. #include "TextServices.h"
  55. #endif
  56.  
  57. #ifndef __TOOLUTILS__
  58. #include <ToolUtils.h>
  59. #endif
  60.  
  61. #ifndef __UTILITIES__
  62. #include "Utilities.h"
  63. #endif
  64.  
  65.  
  66.  
  67. /*****************************************************************************/
  68.  
  69.  
  70.  
  71. extern char        gODOCPermission;
  72. extern Cursor    *gCursorPtr;
  73. extern Boolean    gQuitApplication;
  74.  
  75.  
  76.  
  77. /*****************************************************************************/
  78. /*****************************************************************************/
  79.  
  80. #ifdef applec
  81. #pragma segment DoEvent
  82. #endif
  83.  
  84. /*****************************************************************************/
  85. /*****************************************************************************/
  86.  
  87.  
  88.  
  89. /* Do the right thing for an event.  Determine what kind of event it is, and
  90. ** call the appropriate routines. */
  91.  
  92. void    DoEvent(EventRecord *event)
  93. {
  94.     Point            pt;
  95.     OSErr            err;
  96.     WindowPtr        rww;
  97.     static Boolean    hleQuit = true;
  98.  
  99.     switch(event->what) {
  100.  
  101.         case nullEvent:
  102.             DoIdleTasks(event);
  103.             break;
  104.  
  105.         case mouseDown:
  106.             DoMouseDown(event);
  107.             break;
  108.  
  109.         case autoKey:
  110.         case keyDown:                    /* Check for menukey equivalents. */
  111.             DoKeyDown(event);
  112.             break;
  113.  
  114.         case activateEvt:
  115.             gCursorPtr = nil;            /* Force recalculation of the cursor. */
  116.             DoActivate((WindowPtr)event->message);
  117.             if (TSMTEAvailable()) TSMEvent(event);
  118.             break;
  119.  
  120.         case updateEvt:
  121.             DoUpdate((WindowPtr)event->message);
  122.             break;
  123.  
  124. /* •••••••• */
  125.         case kHighLevelEvent:
  126.             if (GetNextDocumentWindow(nil, 0)) hleQuit = false;    /* If have window, don't auto-force quit. */
  127.             gCursorPtr = nil;                                    /* Force recalculation of the cursor. */
  128.  
  129.             gODOCPermission = fsRdPerm;
  130.             DoHighLevelEvent(event);
  131.             gODOCPermission = fsRdWrPerm;
  132.  
  133.             if (!GetNextDocumentWindow(nil, 0)) hleQuit = false;
  134.                 /* If the high-level event was an 'odoc', then it SHOULD have created a document
  135.                 ** window.  If it didn't, then it wasn't an 'odoc'.  If it wasn't an 'odoc', then
  136.                 ** we were started up in a normal fashion, so therefore don't force quit. */
  137.             if (GetNextDocumentWindow(nil, 'PtMd')) hleQuit = false;
  138.                 /* If a PartMaker document was dropped on us, don't force quit. */
  139.             DoFolders();
  140.             while ((rww = GetNextDocumentWindow(nil, 'rsrc')) != nil) DisposeOneWindow(rww, kClose);
  141.                 /* After the DoFolders call, we're done with 'rsrc' documents, so get rid of them. */
  142.             gQuitApplication |= hleQuit;
  143.             break;
  144.                 /* The changes above (as compared to AppWannabe code for kHighLevelEvent) is to
  145.                 ** handle the various cases of documents being dragged to PartMaker (or not). */
  146. /* •••••••• */
  147.  
  148.         case osEvt:
  149.             gCursorPtr = nil;            /* Force recalculation of the cursor. */
  150.             switch ((event->message >> 24) & 0xFF) {
  151.                     /* Must logical and with 0xFF to get only low byte. */
  152.                     /* High byte of message. */
  153.  
  154.                 case mouseMovedMessage:
  155.                     DoIdleTasks(event);
  156.                     break;
  157.  
  158.                 case suspendResumeMessage:
  159.                         /* Suspend/resume is also an activate/deactivate. */
  160.                     gInBackground = !((event->message) & resumeFlag);
  161.                     CTEConvertClipboard((event->message) & convertClipboardFlag, !gInBackground);
  162.                     DoActivate(FrontWindow());
  163.                     HiliteWindows();
  164.                     break;
  165.             }
  166.             break;
  167.  
  168.         case diskEvt:
  169.             gCursorPtr = nil;            /* Force recalculation of the cursor. */
  170.             if (HiWord(event->message) != noErr) {
  171.                 SetPt(&pt, kDILeft, kDITop);
  172.                 err = DIBadMount(pt, event->message);
  173.             }
  174.             break;        /* It is not a bad idea to at least call DIBadMount
  175.                         ** in response to a diskEvt, so that the user can
  176.                         ** format a floppy. */
  177.     }
  178.  
  179.     DoCursor();
  180.     DoAdjustMenus();
  181. }
  182.  
  183.  
  184.  
  185. /*****************************************************************************/
  186.  
  187.  
  188.  
  189. /* •• Called by DTS.Lib..framework. •• */
  190.  
  191. /* This is called when a window is activated or deactivated. */
  192.  
  193. void    DoActivate(WindowPtr window)
  194. {
  195.     RgnHandle    rgn, oldClip;
  196.     Point        pt;
  197.  
  198.     NotifyCancel();
  199.  
  200.     if (IsAppWindow(window)) {
  201.  
  202.         SetPort(window);
  203.         DoCtlActivate(window);
  204.         DoDrawFrame(window, true);            /* Redraw frame for new activate state. */
  205.  
  206.         CopyRgn(((WindowPeek)window)->contRgn, rgn = NewRgn());
  207.         DiffRgn(rgn, ((WindowPeek)window)->updateRgn, rgn);
  208.             /* Don't draw any part that's already destined to draw due to an update event.
  209.             ** This prevents part of an exposed window from drawing twice, and thus avoids
  210.             ** flickering. */
  211.  
  212.         BeginContent(window);
  213.  
  214.         pt.h = pt.v = 0;
  215.         GlobalToLocal(&pt);
  216.         OffsetRgn(rgn, pt.h, pt.v);
  217.             /* This offset may seem wrong, since the origin may not be 0,0.  It is actually correct,
  218.             ** since at the time of the GlobalToLocal, the port's origin was set to the content origin.
  219.             ** A clearer way to look at this is in two steps:
  220.             ** 1) With the port's origin at 0,0, do a GlobalToLocal on 0,0, and then offset the new clip
  221.             **    by that amount.
  222.             ** 2) Once the port's origin is set correctly, the new clip needs to be offset again, based
  223.             **    on the origin value.  (The clipRgn travels with the origin, unlike the visRgn.)
  224.             ** The above is what automatically happens when you do the GlobalToLocal of 0,0 on the
  225.             ** correct origin.  Offsets are additive.  One step -- no muss, no fuss. */
  226.  
  227.         GetClip(oldClip = NewRgn());
  228.         SetClip(rgn);
  229.         DoDrawControls(window, true);        /* Redraw content scrollbars for new activate state. */
  230.         SetClip(oldClip);
  231.         DisposeRgn(oldClip);
  232.         DisposeRgn(rgn);
  233.  
  234.         EndContent(window);
  235.     }
  236. }
  237.  
  238.  
  239.  
  240.